package com.bensoft.minesweeper.gui;

import com.bensoft.minesweeper.ApplicationController;
import com.bensoft.minesweeper.gui.minesweeperframe.DisplayPanel;
import com.bensoft.minesweeper.gui.minesweeperframe.Grid;
import com.bensoft.minesweeper.gui.minesweeperframe.MenuBar;
import com.bensoft.minesweeper.resources.FileResources;
import com.bensoft.minesweeper.resources.strings.StringResources;
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;

/**
 *
 * @author Ben Wolsieffer
 */
public class MinesweeperFrame extends JFrame implements GUILoader {

    private Grid grid;
    private MenuBar menuBar;
    private DisplayPanel displayPanel;
    private static final String CHEAT_CODE = "XYZZY";
    private static final long CHEAT_THRESHOLD = 500;

    public Grid getGrid() {
        return grid;
    }

    public DisplayPanel getDisplayPanel() {
        return displayPanel;
    }

    public MinesweeperFrame() {
        init();
    }

    private void init() {
        initializeComponents();
        setLayouts();
        addComponents();
        addComponentListeners();
        otherInit();
    }

    @Override
    public void initializeComponents() {
        // must be in this order
        displayPanel = new DisplayPanel();
        grid = new Grid(this);
        menuBar = new MenuBar();

    }

    @Override
    public void setLayouts() {
        setLayout(new BorderLayout());
    }

    @Override
    public void addComponents() {
        setJMenuBar(menuBar);
        add(displayPanel, BorderLayout.NORTH);
        add(grid, BorderLayout.CENTER);
    }

    @Override
    public void addComponentListeners() {
        addKeyListener(new KeyAdapter() {
            private long lastTime;
            private int cheatIndex;

            @Override
            public void keyTyped(KeyEvent e) {
                long currTime = System.currentTimeMillis();
                if (cheatIndex < CHEAT_CODE.length()) {
//                    System.out.println("cheatIndex < CHEAT_CODE.length()");
                    if (e.getKeyChar() == CHEAT_CODE.charAt(cheatIndex)) {
//                        System.out.println("    e.getKeyChar() == CHEAT_CODE.charAt(cheatIndex)");
                        if (currTime - lastTime < CHEAT_THRESHOLD || cheatIndex == 0) {
//                            System.out.println("        currTime - lastTime < CHEAT_THRESHOLD || cheatIndex == 0");
                            cheatIndex++;
                            lastTime = currTime;
                        } else {
//                            System.out.println("Failed threshold check");
                            cheatIndex = 0;
                        }
                    } else {
//                        System.out.println("Failed key char check");
                        cheatIndex = 0;
                    }
                } else if (e.getKeyChar() == KeyEvent.VK_ENTER && currTime - lastTime < CHEAT_THRESHOLD) {
                    ApplicationController.setCheatEnabled(true);
//                    System.out.println("Cheat enabled!!!!!!!!!!!!");
                    lastTime = currTime;
                } else {
//                    System.out.println("Failed enter check, " + e.getKeyCode() + "cheatIndex: " + cheatIndex);
                    cheatIndex = 0;
                }

//                System.out.println("================================");
            }
        });
    }

    @Override
    public void otherInit() {
        FileResources.addThemeChangeListener(new FileResources.ThemeChangeListener() {
            @Override
            public void themeChanged() {
                setIconImage(FileResources.getAppImage());
            }
        });
        setTitle(StringResources.getString("gui.frame.minesweeper.title"));
        setIconImage(FileResources.getAppImage());
        setLocationByPlatform(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        pack();
        setVisible(true);

    }
}
